﻿using System;
using Firestarter.DB.Schema.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI.DatabaseConnection
{
    public class DatabaseConnectionGridAndEditorControl : UserControlWin, IBOGridAndEditorControl
    {
        public readonly IControlFactory _controlFactory = GlobalUIRegistry.ControlFactory;
        private readonly IBOGridAndEditorControl _boGridAndEditorControl;
        private readonly DatabaseConnectionInfoEditorControl _databaseConnectionInfoEditorControl;
        private BusinessObjectCollection<DatabaseConnectionInfo> _databaseConnectionInfos;
        private readonly IButton _testConnectionButton;

        public DatabaseConnectionGridAndEditorControl()
        {
            _databaseConnectionInfoEditorControl = new DatabaseConnectionInfoEditorControl(_controlFactory);
            _boGridAndEditorControl = _controlFactory.CreateGridAndBOEditorControl<DatabaseConnectionInfo>(_databaseConnectionInfoEditorControl);

            _testConnectionButton = SetupTestConnectionButton(_databaseConnectionInfoEditorControl);

            BorderLayoutManager mainLayoutManager = _controlFactory.CreateBorderLayoutManager(this);
            mainLayoutManager.AddControl(_boGridAndEditorControl, BorderLayoutManager.Position.Centre);
        }

        private IButton SetupTestConnectionButton(IControlHabanero panel)
        {
            IButtonGroupControl testConnectionButtonGroupControl = _controlFactory.CreateButtonGroupControl();
            var testConnectionButton = testConnectionButtonGroupControl.AddButton("Test Connection",
                                                                                  TestConnection_ClickHandler);
            testConnectionButtonGroupControl.Dock = DockStyle.Bottom;
            panel.Controls.Add(testConnectionButtonGroupControl);
            return testConnectionButton;
        }
        
        public BusinessObjectCollection<DatabaseConnectionInfo> DatabaseConnectionInfos
        {
            get { return _databaseConnectionInfos; }
            set
            {
                _databaseConnectionInfos = value;
                _boGridAndEditorControl.BusinessObjectCollection = _databaseConnectionInfos;
            }
        }

        public IBusinessObjectCollection BusinessObjectCollection
        {
            set { DatabaseConnectionInfos = (BusinessObjectCollection<DatabaseConnectionInfo>) value; }
        }

        public IGridControl GridControl
        {
            get { return _boGridAndEditorControl.GridControl; }
        }
        public IBOEditorControl IBOEditorControl
        {
            get { return _boGridAndEditorControl.IBOEditorControl; }
        }
        public IButtonGroupControl ButtonGroupControl
        {
            get { return _boGridAndEditorControl.ButtonGroupControl; }
        }

        public IBusinessObject CurrentBusinessObject
        {
            get { return _boGridAndEditorControl.CurrentBusinessObject; }
        }

        public IBOGridAndEditorControl BOGridAndEditorControl
        {
            get { return _boGridAndEditorControl; }
        }

        private void TestConnection_ClickHandler(object sender, EventArgs args)
        {
            var databaseConnectionInfo = CurrentDatabaseConnectionInfo;
            if (databaseConnectionInfo == null) return;
            string message;
            bool isDatabaseConnectionValid = databaseConnectionInfo.IsDatabaseConnectionValid(out message);
            _controlFactory.ShowMessageBox(isDatabaseConnectionValid ? "Connection tested OK" : message);
        }

        public IButton TestConnectionButton
        {
            get { return _testConnectionButton; }
        }

        public DatabaseConnectionInfo CurrentDatabaseConnectionInfo
        {
            get { return CurrentBusinessObject as DatabaseConnectionInfo; }
            set
            {
                //if (value != null) 
                    GridControl.SelectedBusinessObject = value;
                //else
                //{
                //    // This is a workaround for Habanero Grids not allowing the selection of a null value.
                //    var businessObjectCollection = DatabaseConnectionInfos;
                //    GridControl.BusinessObjectCollection = new BusinessObjectCollection<DatabaseConnectionInfo>();
                //    GridControl.SelectedBusinessObject = null;
                //    GridControl.BusinessObjectCollection = businessObjectCollection;
                //}
            }
        }


        // private IPanel GetButtonsPanel()
        //{
        //     IPanel panel = _controlFactory.CreatePanel();
        //     panel.BackColor = Color.DodgerBlue;
        //     panel.Height = 100;
        //     panel.Width = 600;
        //     IButtonGroupControl buttonGroup = _controlFactory.CreateButtonGroupControl();
        //     buttonGroup.AddButton("Test Connection", TestConnection_ClickHandler);
        //     FlowLayoutManager layoutManager= new FlowLayoutManager(panel,_controlFactory);
        //     layoutManager.AddControl(buttonGroup);
        //     layoutManager.NewLine();

        //     return panel;
        // }

        //private IButtonGroupControl GetButtons()
        //{
        //    IButtonGroupControl butz = _boGridAndEditorControl.ButtonGroupControl;
        //    butz.Height = 80;
        //    return butz;
        //}

        //private void SetupOpenFileDialogExtendedTextBoxExtensionFilters()
        //{
        //   // if (_fileDialogExtendedTextBox == null) return;
        //   // _fileDialogExtendedTextBox.InitialDirectory = "";

        //    //_fileDialogExtendedTextBox.FileExtensions.AddExtensionFilter("mdb", "Microsoft Access");
        //   // _fileDialogExtendedTextBox.FileExtensions.AddExtensionFilter("accdb", "Microsoft Access 2007");
        //}

        //private void SetFileDialogExtendedTextBox(IPanelInfo filebasedDatabasePanelInfo)
        //{
        //    PanelInfo.FieldInfo fieldInfo = filebasedDatabasePanelInfo.FieldInfos["Database"];
        //    //_fileDialogExtendedTextBox = fieldInfo.InputControl as OpenFileDialogExtendedTextBox;
        //}

        //private IPanel GetDatabaseConnectionGrid(PanelBuilder panelBuilder)
        //{
        //    _gridPanelInfo = (IPanelInfo) panelBuilder.ControlFactory.CreateReadOnlyGrid().ClassDef.GetPropDef("default");
        //    _gridPanelInfo.BusinessObject = this.DatabaseConnectionInfo;
        //    return _gridPanelInfo.Panel;
        //}

       // public ReverseEngineering ReverseEngineering
       // {
           // get; set;
            //get { return _reverseEngineering; }
            //set
            //{
            //    if (_databaseConnectionInfoRelationship != null)
            //    {
            //        _databaseConnectionInfoRelationship.Updated -= DatabaseConnectionInfoRelationship_Updated;
            //    }
            //    _reverseEngineering = value;
            //    if (_reverseEngineering != null)
            //    {
            //        _databaseConnectionInfoRelationship = _reverseEngineering
            //            .Relationships.GetSingle<DatabaseConnectionInfo>("DatabaseConnectionInfo");
            //        _databaseConnectionInfoRelationship.Updated += DatabaseConnectionInfoRelationship_Updated;
            //    }
            //    UpdateDatabaseConnectionInfo();
            //}
        //}

        //// ReSharper disable InconsistentNaming
        //private void DatabaseConnectionInfoRelationship_Updated(object sender, BOEventArgs<DatabaseConnectionInfo> e)
        //// ReSharper restore InconsistentNaming
        //{
        //    UpdateDatabaseConnectionInfo();
        //}


        //private void TestConnection_ClickHandler(object sender, EventArgs args)
        //{
        //    DatabaseConnectionInfo databaseConnectionInfo = (DatabaseConnectionInfo)_boGridAndEditorControl.IBOEditorControl.BusinessObject;
        //    if (databaseConnectionInfo != null)
        //    {
        //        string message;
        //        bool isDatabaseConnectionValid = databaseConnectionInfo.IsDatabaseConnectionValid(out message);
        //        if (isDatabaseConnectionValid)
        //        {
        //            this.ControlFactory.ShowMessageBox("Connection tested OK");
        //        }
        //        else
        //        {
        //            this.ControlFactory.ShowMessageBox(message);
        //        }
        //    }
        //}
    }
}
